import { Container } from '../Container';
import { EventHandler } from './Common';
import { DataService } from "./DataService";
import { ViewerService } from "./ViewerService";

export class SettingService {
    DataService = Container.defineProperty(DataService);
    ViewerService = Container.defineProperty(ViewerService);
    defaultSetting: any = new SettingData();
    DataChanged: EventHandler = new EventHandler();
    getData(): { setting: SettingData; } {
        if (!this.DataService.Data.setting) {
            this.DataService.Data.setting = {};
        }
        for (const k of Object.keys(this.defaultSetting)) {
            if (this.DataService.Data.setting[k] == undefined) {
                this.DataService.Data.setting[k] = this.defaultSetting[k];
            }
        }
        return this.DataService.Data;
    }
    async updateViewer() {
        let viewer = await this.ViewerService.ViewerPromise;
        let data = this.getData().setting;
        viewer.setLightPreset(parseInt(data.lightPreset as any));
        //@ts-ignore
        viewer.prefs.set("ambientShadows", data.ambientShadows);
        //@ts-ignore
        viewer.impl.dir_light1.intensity = Math.pow(2, data.lightValue);
        //@ts-ignore
        viewer.impl.toggleShadows(data.toggleShadows);
        //@ts-ignore
        viewer.impl.toggleGhosting(data.toggleGhosting);
        //@ts-ignore
        viewer.impl.setFPSTargets(1, data.FPSTargets, 240);
        //@ts-ignore
        viewer.impl.renderer().setAOOptions(data.AOradius, data.AOintensity, data.AOopacity);
        if (!data.backTopColor) {
            data.backTopColor = [13, 29, 58];
        }
        if (!data.backBottomColor) {
            data.backBottomColor = [13, 29, 58];
        }
        if (data.backTopColor && data.backBottomColor) {
            viewer.setBackgroundColor(data.backTopColor[0], data.backTopColor[1], data.backTopColor[2], data.backBottomColor[0], data.backBottomColor[1], data.backBottomColor[2]);
        }
        if (data.ghostingOpacity == undefined) {
            data.ghostingOpacity = 0.04;
        }
        //@ts-ignore
        viewer.impl.fadeMaterial.opacity = data.ghostingOpacity;

        if (data.ghostingColor == undefined) {
            data.ghostingColor = [255, 255, 255];
        }
        //@ts-ignore
        viewer.impl.fadeMaterial.color = new THREE.Color(data.ghostingColor[0], data.ghostingColor[1], data.ghostingColor[2]);

        viewer.impl.invalidate(true, true, true);
    }
}
export class SettingData {
    AOradius: number = 10;
    AOintensity: number = 1;
    AOopacity: number = 1;
    lightPreset: number = 4;
    ambientShadows: boolean = false;
    lightValue: number = 0.3;
    toggleShadows: boolean = false;
    toggleGhosting: boolean = true;
    backTopColor: number[] = [13, 29, 58,];
    backBottomColor: number[] = [13, 29, 58,];
    ghostingOpacity: number = 0.04;
    ghostingColor: number[] = [255, 255, 255,];
    FPSTargets: number = 15;
}